package com.app.twth.ui.tally.sku;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.ItemTallyDetailListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.ui.tally.TallyDetailItemAdapter;
import com.app.twth.ui.tally.TallyDetails;

import java.util.ArrayList;
import java.util.List;


public class TallySkuAdapter extends RecyclerView.Adapter<TallySkuAdapter.MyViewHolder> {
    private int index = -1;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyOnClickListener locationListener;
    private List<TallyDetails> data = new ArrayList<>();

    public TallySkuAdapter(Context mContext, List<TallyDetails> data, MyOnClickListener locationListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
    }

    public void setData(List<TallyDetails> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemTallyDetailListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemTallyDetailListBinding itemBinding;

        public MyViewHolder(ItemTallyDetailListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            TallyDetails tallyLPN = data.get(position);
            itemBinding.tvSku.setText(tallyLPN.getSku() + "");
            itemBinding.tvSkuQty.setText(tallyLPN.getSkuQty() + "");
            itemBinding.tvTallyQty.setText(tallyLPN.getTalliedQty() + "");
            itemBinding.tvLpnQty.setText(tallyLPN.getLpnQty() + "");

            if (tallyLPN.getSkuQty() == null) {
                itemBinding.tvTallyQty.setTextColor(mContext.getResources().getColor(R.color.red));
            } else if (tallyLPN.getTalliedQty() == null) {
                itemBinding.tvTallyQty.setTextColor(mContext.getResources().getColor(R.color.red));
            } else if (tallyLPN.getSkuQty() - tallyLPN.getTalliedQty() == 0) {
                itemBinding.tvTallyQty.setTextColor(mContext.getResources().getColor(R.color.color_666666));
            } else {
                itemBinding.tvTallyQty.setTextColor(mContext.getResources().getColor(R.color.red));
            }

            if (tallyLPN.getLpnQty() == null) {
                itemBinding.ivDown.setVisibility(View.INVISIBLE);
            } else if (tallyLPN.getLpnQty().equals(0)) {
                itemBinding.ivDown.setVisibility(View.INVISIBLE);
            } else {
                itemBinding.ivDown.setVisibility(View.VISIBLE);
            }

            TallyDetailItemAdapter adapter = new TallyDetailItemAdapter(mContext, tallyLPN.getLpnsList(), null);
            LinearLayoutManager layoutManager1 = new LinearLayoutManager(mContext);
            layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
            itemBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
            itemBinding.recyclerviewDetail.setAdapter(adapter);

            itemBinding.ivDown.setOnClickListener(v -> {
                locationListener.OnClick(position);
            });

            if (tallyLPN.isClick()) {
                itemBinding.recyclerviewDetail.setVisibility(View.VISIBLE);
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_up));
            } else {
                itemBinding.recyclerviewDetail.setVisibility(View.GONE);
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_down));
            }
        }
    }

}
